#coding=utf-8
import cv2 as cv
cv.face
def generate():
    # face_cascade = cv.CascadeClassifier('C:\\Users\\asus\\AppData\\Roaming\\Python\\Python37\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml')
    # # eye_cascade = cv.CascadeClassifier('C:\\Users\\asus\\AppData\\Roaming\\Python\\Python37\\site-packages\\cv2\\data\\haarcascade_eye_tree_eyeglasses.xml')
    face_cascade = cv.CascadeClassifier("haarcascade_frontalface_alt2.xml")
    face_cascade.load('C:\\Users\\asus\\AppData\\Roaming\\Python\\Python37\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml')  # 一定要告诉编译器文件所在的具体位置
    eye_cascade = cv.CascadeClassifier('haarcascade_eye_tree_eyeglasses.xml')
    eye_cascade.load('C:\\Users\\asus\\AppData\\Roaming\\Python\\Python37\\site-packages\\cv2\\data\\haarcascade_eye_tree_eyeglasses.xml')  # 一定要告诉编译器文件所在的具体位置

    camera =cv.VideoCapture(0)
    count=0
    while True:
        ret,frame=camera.read()
        gray = cv.cvtColor(frame,cv.COLOR_BGR2GRAY)#将图片转化成灰度
        faces =face_cascade.detectMultiScale(gray,1.3,5)
        for (x,y,w,h) in faces:
            img =cv.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
            f =cv.resize(gray[y:y+h,x:x+w],(200,200))
            cv.imwrite('C:\\Users\\asus\\Pictures\\Camera Roll\\myface\\%s.pgm'%str(count),f)
            count +=1
        cv.imshow('camera',frame)
        if cv.waitKey(1000// 12) & 0xff == ord('q'):
            break
        # camera.release()
    cv.destroyAllWindows()

if __name__ == '__main__':
    generate()

